package org.tinygroup.datasource.jmeter;

import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import performance.jmeter.TpsQueryTest;

/**
 * Created by wangwy on 2017/1/4.
 */
public class JmeterDsQueryTest extends AbstractJavaSamplerClient {
    // 这个方法是用来自定义java方法入参的。
    // params.addArgument("num1","");表示入参名字叫num1，默认值为空。
    //设置可用参数及的默认值；
    public Arguments getDefaultParameters() {
        Arguments params = new Arguments();
        params.addArgument("byName", "false");
        return params;
    }

    //每个线程测试前执行一次，做一些初始化工作；
    public void setupTest(JavaSamplerContext arg0) {
    }

    @Override
    public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
        String isName = javaSamplerContext.getParameter("byName");
        if(isName == null){
            isName = "false";
        }
        boolean isNameBool = Boolean.valueOf(isName);
        SampleResult sr = new SampleResult();
        try {
            sr.setSampleLabel( "dbcluster ds查询数据");
            sr.sampleStart();// jmeter 开始统计响应时间标记
            TpsQueryTest tpsQueryTest = new DbclusterDsQuery();
            tpsQueryTest.doQuery(isNameBool);
            tpsQueryTest.tearDown();
            sr.setSuccessful(true);
            sr.setResponseMessage("success");
        } catch (Exception e) {
            sr.setSuccessful(false);
            sr.setResponseMessage("fail");
            throw new RuntimeException(e);
        } finally {
            sr.sampleEnd();// jmeter 结束统计响应时间标记
        }
        return sr;
    }

    @Override
    public void teardownTest(JavaSamplerContext context) {

        super.teardownTest(context);
    }
}
